package com.hy.convenient.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 用于layui的表格数据返回
 */
public class LayuiToTable implements Serializable {

    public static Map<String, Object> layuiToTable(Object list, int count) {
        return layuiToTable(list, count, "");
    }

    /**
     * 仅仅用于layui的表格数据返回
     *
     * @param list data数据
     * @param count 记录总条数
     * @param msg 错误信息或提示信息
     * @return
     */
    public static Map<String, Object> layuiToTable(Object list, int count, String msg) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", msg);
        result.put("count", count);
        result.put("data", JSONArray.parseArray(JSON.toJSONString(list, SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue)));
        return result;
    }

    /**
     * 业务请求成功
     *
     * @param list  返回数据 是ArrayLsit
     * @param count
     * @return
     */
    public static Map<String, Object> business_suf(Object list, int count) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", CommonData.BUSINESS_SUF);
        result.put("msg", "");
        result.put("count", count);
        result.put("data", JSONArray.parseArray(JSON.toJSONString(list, SerializerFeature.PrettyFormat,
                SerializerFeature.WriteMapNullValue)));
        return result;
    }

    /**
     * 业务请求失败
     *
     * @param msg 返回数据 提示数据
     * @return
     */
    public static Map<String, Object> business_fail(String msg) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", CommonData.BUSINESS_FAIL);
        result.put("msg", msg);
        result.put("count", 0);
        result.put("data", null);
        return result;
    }

    /**
     * 服务器异常返回对应状态
     *
     * @param code
     * @return
     */
    public static Map<String, Object> business_fail(int code) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", code);
        result.put("msg", null);
        result.put("count", 0);
        result.put("data", null);
        return result;
    }
}
